import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { useUserStore } from '@vben/stores';

import { getSimpleContactList } from '#/api/crm/contact';
import { getCustomerSimpleList } from '#/api/crm/customer';
import { getAreaTree } from '#/api/system/area';
import { getSimpleUserList } from '#/api/system/user';

import { $t } from '#/locales'

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  const userStore = useUserStore();
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.crm.contact.contactName'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.crm.contact.contactNamePlaceholder'),
      },
    },
    {
      fieldName: 'ownerUserId',
      label: $t('views.crm.common.ownerUserId'),
      component: 'ApiSelect',
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        disabled: (values) => values.id,
      },
      componentProps: {
        api: () => getSimpleUserList(),
        fieldNames: {
          label: 'nickname',
          value: 'id',
        },
        placeholder: $t('views.crm.common.ownerUserIdPlaceholder'),
      },
      defaultValue: userStore.userInfo?.id,
    },
    {
      fieldName: 'customerId',
      label: $t('views.crm.contact.customer'),
      component: 'ApiSelect',
      rules: 'required',
      componentProps: {
        api: () => getCustomerSimpleList(),
        fieldNames: {
          label: 'name',
          value: 'id',
        },
        placeholder: $t('views.crm.contact.customerPlaceholder')
      },
    },
    {
      fieldName: 'mobile',
      label: $t('views.crm.common.mobile'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.mobilePlaceholder'),
      },
    },
    {
      fieldName: 'telephone',
      label: $t('views.crm.common.telephone'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.telephonePlaceholder'),
      },
    },
    {
      fieldName: 'email',
      label: $t('views.crm.common.email'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.emailPlaceholder'),
      },
    },
    {
      fieldName: 'wechat',
      label: $t('views.crm.common.wechat'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.wechatPlaceholder'),
      },
    },
    {
      fieldName: 'qq',
      label: $t('views.crm.common.qq'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.qqPlaceholder'),
      },
    },
    {
      fieldName: 'post',
      label: $t('views.crm.common.post'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.postPlaceholder'),
      },
    },
    {
      fieldName: 'master',
      label: $t('views.crm.contact.master'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        placeholder: $t('views.crm.contact.masterPlaceholder'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: false,
    },
    {
      fieldName: 'sex',
      label: $t('views.crm.common.sex'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX, 'number'),
        placeholder: $t('views.crm.common.sexPlaceholder'),
      },
    },
    {
      fieldName: 'parentId',
      label: $t('views.crm.contact.parentId'),
      component: 'ApiSelect',
      componentProps: {
        api: () => getSimpleContactList(),
        fieldNames: {
          label: 'name',
          value: 'id',
        },
        placeholder: $t('views.crm.contact.parentIdPlaceholder'),
      },
    },
    {
      fieldName: 'areaId',
      label: $t('views.crm.common.areaId'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: () => getAreaTree(),
        fieldNames: { label: 'name', value: 'id', children: 'children' },
        placeholder: $t('views.crm.common.areaIdPlaceholder'),
      },
    },
    {
      fieldName: 'detailAddress',
      label: $t('views.crm.common.detailAddress'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.detailAddressPlaceholder'),
      },
    },
    {
      fieldName: 'contactNextTime',
      label: $t('views.crm.followUp.contactNextTime'),
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
        placeholder: $t('views.crm.followUp.contactNextTimePlaceholder'),
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'customerId',
      label: $t('views.crm.common.customer'),
      component: 'ApiSelect',
      componentProps: {
        api: () => getCustomerSimpleList(),
        fieldNames: {
          label: 'name',
          value: 'id',
        },
        placeholder: $t('views.crm.common.customerPlaceholder'),
      },
    },
    {
      fieldName: 'name',
      label: $t('views.crm.contact.contactName'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.contact.contactNamePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'mobile',
      label: $t('views.crm.common.mobile'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.mobilePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'telephone',
      label: $t('views.crm.common.telephone'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.telephonePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'wechat',
      label: $t('views.crm.common.wechat'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.wechatPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'email',
      label: $t('views.crm.common.email'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.crm.common.emailPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('views.crm.contact.contactName'),
      fixed: 'left',
      minWidth: 240,
      slots: { default: 'name' },
    },
    {
      field: 'customerName',
      title: $t('views.crm.contact.customer'),
      fixed: 'left',
      minWidth: 240,
      slots: { default: 'customerName' },
    },
    
    {
      field: 'mobile',
      title: $t('views.crm.common.mobile'),
      minWidth: 120,
    },
    {
      field: 'telephone',
      title: $t('views.crm.common.telephone'),
      minWidth: 130,
    },
    {
      field: 'email',
      title: $t('views.crm.common.email'),
      minWidth: 180,
    },
    {
      field: 'post',
      title: $t('views.crm.common.post'),
      minWidth: 120,
    },
    {
      field: 'areaName',
      title: '地址',
      minWidth: 120,
    },
    {
      field: 'detailAddress',
      title: $t('views.crm.common.detailAddress'),
      minWidth: 180,
    },
    {
      field: 'master',
      title: $t('views.crm.contact.master'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'parentId',
      title: $t('views.crm.contact.parentId'),
      minWidth: 120,
      slots: { default: 'parentId' },
    },
    {
      field: 'contactNextTime',
      title: $t('views.crm.followUp.contactNextTime'),
      formatter: 'formatDateTime',
      minWidth: 180,
    },
    {
      field: 'sex',
      title: $t('views.crm.common.sex'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_USER_SEX },
      },
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 200,
    },
    {
      field: 'contactLastTime',
      title: $t('views.crm.followUp.contactLastTime'),
      formatter: 'formatDateTime',
      minWidth: 180,
    },
    {
      field: 'ownerUserName',
      title: $t('views.crm.common.ownerUserId'),
      minWidth: 120,
    },
    {
      field: 'ownerUserDeptName',
      title: $t('views.crm.common.ownerUserDept'),
      minWidth: 120,
    },
    {
      field: 'updateTime',
      title: $t('common.updateTime'),
      formatter: 'formatDateTime',
      minWidth: 180,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      formatter: 'formatDateTime',
      minWidth: 180,
    },
    {
      field: 'creatorName',
      title: $t('views.common.creatorName'),
      minWidth: 120,
    },
    {
      title: $t('common.action'),
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
